package com.example.com.skills_utf8;

import java.util.Timer;
import java.util.TimerTask;

import com.utils.Utils1;
import com.utils.cHighTimer;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class _48Activity extends Activity {

	
	LinearLayout layout_content , layout_content1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_48);
		
		layout_content = (LinearLayout)findViewById(R.id.layout_content);
		layout_content1 = (LinearLayout)findViewById(R.id.layout_content1);
		
		//CircleView circle = new CircleView(this);
		
		/*
		int[] ScreenSize = Utils1.getScreenSize(this);
		for(int i =0; i<5; i++){
			 
			CircleSurfaceView circle = new CircleSurfaceView(this);
			circle.SetPoint(new Point(200,200+i*100));
			layout_content.addView(circle);
		}
		*/
		
		///*
		CircleSurfaceView circle1 = new CircleSurfaceView(this);
		circle1.SetPointCenter(new Point(200,200+1*100));
		layout_content.addView(circle1);
		
	 
		CircleSurfaceView circle = new CircleSurfaceView(this);
		circle.SetPointCenter(new Point(200,200+1*100));
		layout_content1.addView(circle);
		//*/
		
	}
	
}


//  用普通的view写，动画不够流程
class CircleView extends View
{
	
	
	private int width_measure = 100;
	private int height_measure = 100;
	

	private Paint mPaintCircle;
	private Point mPointCenter;
	private int mRadius;
	
	private int screen_width ;
	private int screen_height ;
	
	private Context mContext;
	private Paint mPaintRing;
	
	private int mCircleRadius = 10;
	private RectF mRectRing = new RectF();
	
	

	public CircleView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		
		init();
	}

	public CircleView(Context context, AttributeSet set){
		super(context,set);
		mContext = context;
		
		init();
		
	}
	 
	void init(){
		
		mPaintCircle = new Paint();
		mPaintCircle.setColor( Color.parseColor("#eeeeee") );
		
		mPaintRing = new Paint();
		mPaintRing.setColor( Color.parseColor("#111111"));
		mPaintRing.setStyle(Style.STROKE);
		mPaintRing.setStrokeWidth(5);
		
		mPointCenter = new Point();
		mPointCenter.x = 200;
		mPointCenter.y = 200;
	
		mRadius = 50;
		
		
		//screen_width = Utils1.getScreenSize(mContext)[0];
		//screen_height = Utils1.getScreenSize()
		
		TimerTask_Circle = new TimerTaskCircle();//(mRadius);
		 timer.schedule(TimerTask_Circle, 1000, 1000); // 1s后执行task,经过1s再次执行  
	}
	
	/*
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
		
	}
	*/
	
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		// TODO Auto-generated method stub
		super.onLayout(changed, left, top, right, bottom);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		
		
		canvas.drawCircle(mPointCenter.x, mPointCenter.y, mRadius, mPaintCircle);
		
		canvas.drawOval(mRectRing, mPaintRing);
		
		
		
		
	}
	
	
	
	
	
	
	 /**
     * Determines the width of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {

        	result += getPaddingLeft() + getPaddingRight();
        	result +=width_measure;
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    }

    /**
     * Determines the height of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
        	result += getPaddingTop() + getPaddingBottom();
        	result += height_measure;
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
    
    
    
    // 1）android定时器，不断绘制；
    // 2) 加载帧动画,不过如果采取这样,就不用自定义view了;
    
    
    Timer timer = new Timer();
    TimerTaskCircle TimerTask_Circle ;
    class TimerTaskCircle extends TimerTask
    {

    	int Radius = mRadius;
		int alpha = 0xff;;
    	
		/*
		public TimerTaskCircle(int Radius){
			this.Radius = Radius;
		}
		*/
		
		@Override
		public void run() {
			// TODO Auto-generated method stub

			if(Radius > mRadius*3){
				Radius = mRadius;
				
			}
			else{
				Radius+=10;
			}
        	
			if(alpha < 0x11){
				alpha = 0xff;
			}
			
        	mRectRing = new RectF();      
        
        	mRectRing.left =mPointCenter.x-Radius;
        	mRectRing.top =mPointCenter.y-Radius;
        	mRectRing.right =mPointCenter.x+Radius;
        	mRectRing.bottom =mPointCenter.y+Radius;
        	
        	
        	System.out.println("mRectRing:"+mRectRing.toString());
        	
        	alpha -=Radius;
        	mPaintRing.setColor(  Color.argb(alpha, 11, 11, 11) );
        	
        	
            postInvalidate();
		}
    	
    };
    
     
	
	
};



class CircleSurfaceView extends SurfaceView implements SurfaceHolder.Callback2, Runnable
{


	private Paint mPaintCircle;
	private Point mPointCenter;
	private int mRadius = 50;
	
	private Context mContext;
	private Paint mPaintRing;
	
	private int mCircleRadius = 10;
	private RectF mRectRing = new RectF();
	
	private int Radius = mRadius;
	private int alpha = 0xff;
	
	
	private Rect mSize = new Rect();
	private cHighTimer mHighTimer = new cHighTimer();
	
	
	SurfaceHolder mHolder;
	
	
	private boolean bEnable = true;
	
	
	
	
	public CircleSurfaceView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		mContext = context;
		
		
		mHolder = this.getHolder();
		mHolder.addCallback(this);
		
		
	
	}
	
	public void SetPointCenter(Point center){
		mPointCenter = center;
		
	}
	
	
	void init(){
		
		if(mPointCenter == null){
			mPointCenter = new Point(200,200);
		}
		
		mPaintCircle = new Paint();
		mPaintCircle.setColor( Color.parseColor("#eeeeee") );
		
		mPaintRing = new Paint();
		mPaintRing.setColor( Color.parseColor("#111111"));
		mPaintRing.setStyle(Style.STROKE);
		mPaintRing.setStrokeWidth(5);
		
	
		mHighTimer.start_counter();
	 
	}
	

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		init();
		new Thread(this).start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		bEnable = false;
	}

	@Override
	public void surfaceRedrawNeeded(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		for(;;)
		{
			float elapsed = mHighTimer.get_elapsed_time();
			if( mHighTimer.controlFPS_Return(elapsed, 33) ){
				synchronized (mHolder) {
					if(bEnable)
					{
						Update();
						Draw();
					}
					else{
						break;
					}
					
				}	
			}
			
		}
		
		
	}	
	
	
	void Update(){
		
		if(Radius > mRadius*3 || alpha < 0x10){
			Radius = mRadius;
			
			alpha = 0xff;
		}
		else{
			Radius+=1;
		}
    	
		 
    	mRectRing = new RectF();      
    
    	mRectRing.left =mPointCenter.x-Radius;
    	mRectRing.top =mPointCenter.y-Radius;
    	mRectRing.right =mPointCenter.x+Radius;
    	mRectRing.bottom =mPointCenter.y+Radius;
    	
    	
    	System.out.println("mRectRing:"+mRectRing.toString());
    	
    	alpha -=5;
    	mPaintRing.setColor(  Color.argb(alpha, 11, 11, 11) );
	}
	
	
	
	void Draw(){
		
		/*
		Rect size = new Rect();
		size.left =(int) mRectRing.left;
		size.top = (int) mRectRing.top;
		size.right = (int) mRectRing.right;
		size.bottom = (int) mRectRing.bottom;
		Canvas canvas = mHolder.lockCanvas(size);
		*/
		Canvas canvas = mHolder.lockCanvas();
		if(canvas == null)
			return;
		
		canvas.drawColor(Color.GRAY); 
		
    	
		canvas.drawCircle(mPointCenter.x, mPointCenter.y, mRadius, mPaintCircle);
		canvas.drawOval(mRectRing, mPaintRing);
    	
		
		mHolder.unlockCanvasAndPost(canvas);
		
		
	}
	
 
	
 
	
};








